//
//  TopViewController.swift
//  MyLottery
//
//  Created by wujunjie on 2018/7/18.
//  Copyright © 2018年 wujunjie. All rights reserved.
//

import UIKit
let loginSuccessNotificationKey = "loginSuccessNotificationKey"
class TopViewController: UIViewController {
    var isLogin : Bool {
        return UserInfoModel.userAccount.isLogin
    }
    var visitorView : VisitorView?
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess), name: Notification.Name(rawValue: loginSuccessNotificationKey), object:nil)
        setupUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    deinit  {
        NotificationCenter.default.removeObserver(self)
    }
}

extension TopViewController{
    private  func setupUI(){
        if !isLogin && visitorView == nil {
             visitorView = VisitorView()
            visitorView?.delegate = self
            view.addSubview(visitorView!)
        }
    }
}

extension TopViewController : VisitorViewProtocol{
    ///实现注册代理
    func regist() {
        
    }
    ///实现登录代理
    func login() {
        let oAuthVC = OAuthViewController()
        let navVC = BaseNavViewController(rootViewController: oAuthVC)
        present(navVC, animated: true) {
                
        }
    }
    ///登录成功
    @objc private func loginSuccess(){
        ///重要登录成功了
        visitorView?.removeFromSuperview()
        visitorView = nil
    }
}
